import sys
import os
from screen_util import ScreenUtil
from ai_util import AiUtil
import config
import pyautogui
from draw_util import DrawUtil
import time

# 获取屏幕尺寸
# 元组类型的返回值
screen_width, screen_height = pyautogui.size()
# 获取屏幕宽高
print("屏幕宽度:", screen_width)
print("屏幕高度:", screen_height)

def is_windows():
    if sys.platform.startswith('win'):
        return True
    return False

def is_mac():
    if sys.platform.startswith('darwin'):
        return True
    return False

def is_linux():
    if sys.platform.startswith('linux'):
        return True
    return False

def click_image(image_path):
    # ScreenUtil.pc_screenshot()
    # match_result = AiUtil.find_sift(config.screenshot_save_path, image_path)
    # print(f"match_result = {match_result}")
    # center = match_result["center"]
    # pyautogui.click(center[0], center[1])

    # 定位图在屏幕上的坐标位置
    point = pyautogui.locateCenterOnScreen(image=image_path)
    if point is None:
        return False
    else:
        pyautogui.click(point[0], point[1])
        time.sleep(0.2)
        return True

def click_text(text):
    ScreenUtil.pc_screenshot()
    result = AiUtil.find_text(config.screenshot_save_path, text)
    if result is None:
        print(f"find_text=[{text}] result Not Found")
        return False
    else:
        print(f"find_text=[{text}] result = {result}")
        points = result[0]
        center = DrawUtil.cal_center(points)
        pyautogui.click(center[0], center[1])
        return True

def write(text, enter=True):
    # 给一点时间切换到另一个程序或窗口，比如记事本
    time.sleep(1)

    # # 将中文复制到剪贴板
    # pyperclip.copy(text)
    #
    # # 模拟按下粘贴
    # paste()

    unicode_bytes = text.encode('utf-8')

    # 输入一段文字
    pyautogui.write(unicode_bytes)
    print(f"输入文字: {text}")

    if enter:
        # 按下然后释放enter键
        print("按下然后释放enter键")
        # pyautogui.press('enter')

    # 也可以组合使用，比如按下ctrl+c（复制）
    # pyautogui.hotkey('ctrl', 'c')

def paste():
    if os.name == 'nt':
        # 'Windows'
        pyautogui.hotkey('Ctrl', 'V')
    elif os.name == 'posix':
        # 'Linux/Unix/Mac OS X'
        pyautogui.keyDown('command')
        pyautogui.keyDown('v')
        time.sleep(0.1)
        pyautogui.keyUp('v')
        pyautogui.keyUp('command')